package com.xmotor.testmvcintercepter.controller;

import com.xmotor.testmvcintercepter.pojo.User;
import com.xmotor.testmvcintercepter.vo.TestVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/test1")
    @ResponseBody
    public Object test1(@RequestAttribute("user")User user, @RequestBody TestVO testVO){
        Map<String, Object> map = new HashMap<>();
        System.out.println("user:"+user);
        System.out.println("testVO:"+testVO);
        map.put("user",user);
        map.put("testVO",testVO);
        return map;
    }

    @RequestMapping("test2")
    @ResponseBody
    public void testGetQueryParam(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String queryString = request.getQueryString();
        System.out.println(queryString);
    }

    //GET: localhost:8080/controller/test2?a=a&b=b&c=c  ----->  a=a&b=b&c=c
    //POST param中设a=a，b=b，c=c: localhost:8080/controller/test2   ----->  a=a&b=b&c=c url自动拼成localhost:8080/controller/test2?a=a&b=b&c=c
    //POST header中设a=a，b=b，c=c: localhost:8080/controller/test2  ----->  null
    //POST body中设a=a，b=b，c=c: localhost:8080/controller/test2  ----->  null

    @RequestMapping("test3")
    public ModelAndView testModelAndView(){
        return new ModelAndView("redirect:https://www.hao123.com/");
//        return new ModelAndView(new RedirectView("https://www.hao123.com/"));
    }
}
